package com.deyuanyun.pic.pipe.controller;

import com.deyuanyun.pic.common.dto.PageJsonBean;
import com.deyuanyun.pic.common.util.ajax.AjaxSupport;
import com.deyuanyun.pic.common.util.converter.BeanConverter;
import com.deyuanyun.pic.common.util.poi.ExcelUtil;
import com.deyuanyun.pic.pipe.controller.dto.PipeBaseQuery;
import com.deyuanyun.pic.pipe.controller.vo.merge.GwwDetectionVO;
import com.deyuanyun.pic.pipe.controller.vo.merge.GwwMergeVO;
import com.deyuanyun.pic.pipe.controller.vo.merge.GwwVO;
import com.deyuanyun.pic.pipe.enumeration.PipeExportEnum;
import com.deyuanyun.pic.pipe.service.GwwService;
import com.deyuanyun.pic.settings.util.DownExcelUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * “环焊缝”控制类（包含环焊缝 和 无损检测焊缝）
 *
 * @作者:严祥
 * @创建时间: 2015年9月14日
 * @类名称: PipeGwwController
 *
 */
@Controller
@RequestMapping("/pipe/gww")
public class GwwController extends PipeBaseController {

    @Resource
    private GwwService gwwService;

    /**
     * 跳转到 环焊缝 页面
     *
     * @return
     */
    @RequestMapping("/toGWW")
    public ModelAndView toGWW() {
        ModelAndView mv = new ModelAndView();
        mv.setViewName("/basedata/pipe/pipeGWW");
        return mv;
    }


    /**
     * 根据条件分页 合并查询 弯头信息（包含环焊缝 和 环焊缝无损检测信息）
     *
     * @param pipeBaseQuery
     */
    @RequestMapping("/query.json")
    @ResponseBody
    public void query(PipeBaseQuery pipeBaseQuery) {
        PageJsonBean jsonBean = new PageJsonBean();
        jsonBean.setList(new GwwMergeVO().fromDomainList(gwwService.queryGwwMerge(pipeBaseQuery)));
        AjaxSupport.sendSuccessText("success", jsonBean);
    }

    /**
     * 根据id查询 环焊缝 详情
     *
     * @param id
     */
    @RequestMapping("/findGww.json")
    @ResponseBody
    public void findGww(String id) {
        GwwVO gwwVO = new GwwVO();
        gwwVO.fromDomain(gwwService.findGww(id));
        AjaxSupport.sendSuccessText("success", gwwVO);
    }
    /**
     * 根据id查询 环焊缝无损检测信息 详情
     *
     * @param id
     */
    @RequestMapping("/findGwwDetection.json")
    @ResponseBody
    public void findGwwDetection(String id) {
        GwwDetectionVO gwwDetectionVO = new GwwDetectionVO();
        AjaxSupport.sendSuccessText("success", gwwDetectionVO.fromDomainList(gwwService.queryGwwDetection(id)));
    }

    /**
     * 根据条件 导出“环焊缝”和“环焊缝无损检测信息”
     *
     * @param pipeBaseQuery
     * @return
     */
    @RequestMapping("/export")
    @ResponseBody
    public void export(PipeBaseQuery pipeBaseQuery) throws Exception{

        List<GwwVO> gwwList = new GwwVO().fromDomainList(gwwService.queryGww(pipeBaseQuery));
        List<GwwDetectionVO> GwwDetectionList = new GwwDetectionVO().fromDomainList(gwwService.queryGwwDetection(pipeBaseQuery));

        // 环焊缝信息
        List<ExcelUtil.SheetParam> list = new ArrayList<ExcelUtil.SheetParam>();
        String[] columnKeys =  {"pipeLineName", "gwwNumber", "mileage", "wpsNumber", "assemblyCompany",
                "constructionDate","recorder", "recordTime", "remarks"};
        list.add(new ExcelUtil.SheetParam(columnKeys, BeanConverter.toListMap(gwwList), 1));
        // 环焊缝无损检测信息
        String[] lineColumnKeys = {"pipeLineName", "gwwNum", "ndtType", "repairind", "testingCompany",
                "testDate", "testingReport", "recorder", "recordTime", "remark"};
        list.add(new ExcelUtil.SheetParam(lineColumnKeys, BeanConverter.toListMap(GwwDetectionList), 1));

        PipeExportEnum pipeExportEnum = PipeExportEnum.GWW;
        DownExcelUtil.export(pipeExportEnum.getFileName(), pipePath+ pipeExportEnum.getTempPath(), list);
    }

}
